﻿namespace Cos.Core.Model.Content
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class Menu : DomainModelBase
    {
        private ICollection<Menu> subMenu;
        private ISet<string> roles;

        public Menu()
        {
            this.subMenu = new List<Menu>();
            this.roles = new HashSet<string>();
        }

        public Menu Parent { get; set; }
        public int NavId { get; set; }
        public int PageId { get; set; }
        public string PageName { get; set; }
        public string TemplateName { get; set; }
        public string NavigateUrl { get; set; }
        public bool IsVisible { get; set; }
        public bool IsPageInMenu { get; set; }
        public DateTime LastModified { get; set; }

        public IEnumerable<string> Roles
        {
            get { return this.roles; }
        }

        public IEnumerable<Menu> SubMenu
        {
            get { return this.subMenu; }
        }

        /**
         * Adds a child menu to this menu
         * @param menuVo The child menu to add
         */
        public void AddMenu(Menu menu)
        {
            this.subMenu.Add(menu);
        }

        /**
         * Adds a role
         * 
         * @param role The role to add
         */
        public void AddRole(string role)
        {
            this.roles.Add(role);
        }

        /**
         * Checks if this entry is secured or not
         * 
         * @return true if it is secured, false otherwise
         */
        public bool IsSecured()
        {
            return this.roles != null && this.roles.Count > 0;
        }

        /**
         * Checks this entry is available
         * 
         * @param authRoles The roles to check for
         * @return true if it is available
         */
        public bool IsAuthorized(IEnumerable<string> authRoles)
        {
            if (this.roles == null || this.roles.Count == 0)
            {
                return true;
            }

            return authRoles.Any(role => this.roles.Contains(role));
        }
    }
}
